import { Inject, Service } from "typedi";
import { SitesList, SitesOptions } from "client/sitesVo";
import {
  repoSiteInterface,
  REPO_SITE_INTERFACE,
} from "infrastructure/rpc/siteRepo";
import {
  repoConfigInterface,
  REPO_CONFIG_INTERFACE,
} from "infrastructure/rpc/configRepo";
import {
  REPO_PRODUCT_INTERFACE,
  repoProductInterface,
} from "infrastructure/rpc/productRepo";
import { RES_INFO, ERR_ALL_PROMISE } from "config/const";
import { HttpParams } from "client/configVo";

export const ACCOUNT_INTERFACE = "accountInterface";

export interface accountInterface {
  getOptions(opts: HttpParams): Promise<SitesOptions>;
  getList(opts: HttpParams): Promise<SitesList[] | any>;
  get_create_info(opts: HttpParams): Promise<SitesList[] | any>;
  create(opts: HttpParams): Promise<SitesList[]>;
  update(opts: HttpParams): Promise<SitesList[]>;
  delete(opts: HttpParams): Promise<SitesList[]>;
  getHelp(opts: HttpParams): Promise<SitesList[]>;
  getHost(opts: HttpParams): Promise<SitesList[]>;
  updateVerify(opts: HttpParams): Promise<SitesList[]>;
  updateOrign(opts: HttpParams): Promise<SitesList[]>;
  updateStatus(opts: HttpParams): Promise<SitesList[]>;
  getZjzOptions(opts: HttpParams): Promise<SitesList[]>;
}

export interface renderDataEntity {
  [key: string]: any | undefined;
  username: string;
  age: number;
}

@Service(ACCOUNT_INTERFACE)
export class Account implements accountInterface {
  @Inject(REPO_SITE_INTERFACE) private readonly siteRepo: repoSiteInterface;
  @Inject(REPO_CONFIG_INTERFACE)
  private readonly configRepo: repoConfigInterface;
  @Inject(REPO_PRODUCT_INTERFACE)
  private readonly productRepo: repoProductInterface;

  // 获取表格列表
  getList(opts: HttpParams): Promise<SitesList | any> {
    const { request, response, userInfo, query } = opts;
    const params = query || request.body;

    return new Promise(async (resolve, reject) => {
      const sitesInfo: any = await this.siteRepo.getSites(opts);
      if (sitesInfo?.no !== 0) return resolve(sitesInfo);

      const siteList = sitesInfo?.data?.list || [];
      if (!siteList?.length || params.no_detail) return resolve(sitesInfo);

      // 通过id查店铺信息
      const ids = siteList.map((item: any) => item.account_id);

      const resList = await Promise.all([
        this.getListDetail({
          ...opts,
          query: { name: "id", op: "IN", value: [...new Set(ids)].join(",") },
        }),
        this.get_create_info({ ...opts, query: { limit: 100, ids } }),
      ]);
      const errRes = ERR_ALL_PROMISE(resList);
      if (errRes) return resolve(errRes);
      const [detailInfo, createInfo] = resList;

      // 整合数据
      const detailList: any = detailInfo?.data || [];
      const createList: any = createInfo?.data?.list || [];
      const detailObj: any = {};
      const createObj: any = {};

      detailList.forEach((item: any) => (detailObj[item.id] = item));
      createList.forEach((item: any) => {
        const account = item.account || {};
        createObj[account.id] = account;
      });

      siteList.forEach((item: any) => {
        item.detail = createObj[item.account_id] || {};
        item.detail.states = detailObj[item.account_id]?.states || [];
      });
      resolve(sitesInfo);
    });
  }

  // 通过查询条件,去订单中心获取账号列表详情
  getListDetail(opts: HttpParams): Promise<SitesList | any> {
    return this.productRepo.getSitesDetail(opts);
  }

  // 获取列表查询下拉
  async getOptions(opts: HttpParams): Promise<SitesOptions | any> {
    const { request, response, userInfo } = opts;
    return new Promise(async (resolve, reject) => {
      const resList = await Promise.all([this.configRepo.getOrgan(opts)]);
      const errRes = ERR_ALL_PROMISE(resList);
      if (errRes) return resolve(errRes);

      const [organRes] = resList;
      resolve(
        RES_INFO({
          data: {
            organList: organRes?.data || [],
          },
        })
      );
    });
  }

  // 获取创建时的账号信息
  async get_create_info(opts: HttpParams): Promise<SitesList[] | any> {
    return this.siteRepo.get_create_info(opts);
  }

  // 创建店铺
  async create(opts: HttpParams): Promise<SitesList[] | any> {
    return this.siteRepo.create(opts);
  }

  // 更新店铺
  async update(opts: HttpParams): Promise<SitesList[] | any> {
    return this.siteRepo.update(opts);
  }

  // 删除店铺
  async delete(opts: HttpParams): Promise<SitesList[] | any> {
    return this.siteRepo.delete(opts);
  }

  // 拿到帮助文档地址
  async getHelp(opts: HttpParams): Promise<SitesList[] | any> {
    return this.productRepo.getHelp(opts);
  }

  // 获取店铺后台域名列表
  async getHost(opts: HttpParams): Promise<SitesList[] | any> {
    return this.siteRepo.getHost(opts);
  }

  // 批量更新专利校验设置
  async updateVerify(opts: HttpParams): Promise<SitesList[]> {
    return this.siteRepo.updateVerify(opts);
  }

  // 批量变更店铺店铺归属
  async updateOrign(opts: HttpParams): Promise<SitesList[]> {
    return this.siteRepo.updateOrign(opts);
  }

  // 批量设置店铺状态
  async updateStatus(opts: HttpParams): Promise<SitesList[]> {
    return this.siteRepo.updateStatus(opts);
  }

  // 获取自建站域名下拉
  async getZjzOptions(opts: HttpParams): Promise<SitesList[]> {
    return this.siteRepo.getZjzOptions(opts);
  }
}
